package ar.com.photo_admin.domain
import ar.com.photo_admin.domain.price_strategies.*;
import grails.plugins.springsecurity.Secured

@Secured(['ROLE_PHOTOGRAPHER'])
class PhotographerDiscountController {

    def photographerConfigurationService
	
	def CHECKED_DEFAULT_NUM = 1;
	def CHECKED_AMOUNT_PRICE_NUM = 3;
	def CHECKED_AMOUNT_PHOTOS_NUM = 2;
	
	def springSecurityService
	
    def index() { 
		redirect(action: "configuration")
	}
		
	def configuration() {
		def priceStratPhotos = new PriceStrategyDiscountOverAmountPhotos();
		def priceStratPrice = new PriceStrategyDiscountOverAmountPrice(); 								
		
		int currentDiscount = 0;
		
		// TODO ver como se puede generalizar esto.
		// Poner el código dentro de la estrategia como atributo estático. Para luego hacer: currentDiscount = photographerInstance.priceStrategy.code -- Nico
		def photographerInstance = springSecurityService.getCurrentUser()
		if (photographerInstance.priceStrategy.class == priceStratPhotos.class){
			priceStratPhotos = photographerInstance.priceStrategy;
			currentDiscount = CHECKED_AMOUNT_PHOTOS_NUM;
		} else if (photographerInstance.priceStrategy.class == priceStratPrice.class){
			priceStratPrice = photographerInstance.priceStrategy;			
			currentDiscount = CHECKED_AMOUNT_PRICE_NUM;
		} else {
			currentDiscount = CHECKED_DEFAULT_NUM;
		}
		
		[priceStratPrice: priceStratPrice, priceStratPhotos: priceStratPhotos, currentDiscount : currentDiscount]
	}
	
	def save() {
		def photographerInstance  = springSecurityService.getCurrentUser()
		photographerInstance.properties = params
		
		log.info "Tipo de descuento:" + params.discountType
		
		def isPhotosStrat = false;
		def isPriceStrat = false;
		
		def strategy = null;
		switch (new Integer(params.discountType)) {
			case CHECKED_DEFAULT_NUM:
				strategy = new PriceStrategy();
				break
			case CHECKED_AMOUNT_PRICE_NUM:
				strategy = new PriceStrategyDiscountOverAmountPrice();
				strategy.properties = params
				isPriceStrat = true;
				break
			case CHECKED_AMOUNT_PHOTOS_NUM:
				strategy = new PriceStrategyDiscountOverAmountPhotos();
				strategy.properties = params
				isPhotosStrat = true;
				break
			default:
				log.info "ERROR"
		 }
		
		//Se mantiene la estrategia anterior para luego borrarla	
		def oldStrategy = photographerInstance.priceStrategy;
		
		strategy.photographer = photographerInstance;
		if (!strategy.validate()){
			def priceStrat = isPriceStrat ? strategy : new PriceStrategyDiscountOverAmountPrice()
			def photosStrat = isPhotosStrat ? strategy : new PriceStrategyDiscountOverAmountPhotos()
			render(view: "configuration", model: [priceStratPrice: priceStrat, priceStratPhotos: photosStrat, currentDiscount : new Integer(params.discountType)])
			return
		} 
		else{
			//Asigno la nueva estrategia al fotografo
			photographerInstance.priceStrategy = strategy;
			if(!photographerInstance.save(flush: true)){
				//Nunca deberia entrar aca ya que el unico cambio que se produce es en la estrategia.
				def priceStrat = isPriceStrat ? strategy : new PriceStrategyDiscountOverAmountPrice()
				def photosStrat = isPhotosStrat ? strategy : new PriceStrategyDiscountOverAmountPhotos()
				render(view: "configuration", model: [priceStratPrice: priceStrat, priceStratPhotos: photosStrat, currentDiscount : new Integer(params.discountType)])
				return
			}
		}
							
		//Borro la estrategia anterior.
		oldStrategy.delete();
		
		flash.message = message(code: 'photographer.discounts.update')
		redirect(action:"configuration")
	}
}
